package com.example.treehole;

import android.content.Intent;
import android.view.View;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.example.treehole.base.BaseBindingActivity;
import com.example.treehole.bean.Post;
import com.example.treehole.databinding.ActivitySendPostBinding;
import com.example.treehole.util.Api;
import com.example.treehole.util.HttpUtil;
import com.example.treehole.util.SomeUtil;
import com.gyf.immersionbar.ImmersionBar;

import java.io.File;
import java.util.HashMap;

/**
 * 帖子界面
 */
public class SendPostActivity extends BaseBindingActivity<ActivitySendPostBinding> {

    @Override
    protected void initListener() {
        viewBinder.ivImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(Intent.ACTION_GET_CONTENT).setType("image/*"), 100);
            }
        });
        viewBinder.send.setOnClickListener(view -> {
            String title = viewBinder.etTitle.getText().toString().trim();
            String content = viewBinder.etContent.getText().toString().trim();
            if (title.isEmpty()) {
                toast("标题为空");
                return;
            }
            if (content.isEmpty()) {
                toast("内容为空");
                return;
            }
            HashMap<String, Object> params = new HashMap<>();
            if (post != null) {
                params.put("id", post.id);
            }
            params.put("userid", App.user.id);
            params.put("time", SomeUtil.getTime());
            params.put("file", imagePath);
            params.put("type", viewBinder.spinner.getSelectedItem());
            params.put("content", content);
            params.put("title", title);
            params.put("anonymous", viewBinder.cbAnonymous.isChecked());

            HttpUtil.post(post != null ? Api.UPDATE_POST : Api.SEND_POST, params, new HttpUtil.OnNetStringListener() {
                @Override
                public void success(String data, String message) {
                    toast(data);
                    dismissLoading();
                    finish();
                }

                @Override
                public void error(String error) {
                    toast(error);
                    dismissLoading();
                }
            });
        });
    }

    private Post post;

    @Override
    protected void initData() {
        ImmersionBar.with(this).titleBarMarginTop(findViewById(R.id.titleBar)).barColor(R.color.colorPrimary).init();
        post = (Post) getIntent().getSerializableExtra("post");
        String image = (String) getIntent().getStringExtra("image");
        if (image != null) {
            imagePath = image;
            loadImage(image, viewBinder.ivImage);
        }
        if (post != null) {

            viewBinder.etTitle.setText(post.title);
            viewBinder.etContent.setText(post.content);
        }
    }

    private String imagePath = "";

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 100) {
                imagePath = SomeUtil.getImagePath(this, data.getData());
                Glide.with(SendPostActivity.this).load(imagePath).into(viewBinder.ivImage);
            } else {
                imagePath = SomeUtil.getVideoPath(this, data.getData());
                Glide.with(SendPostActivity.this).load(imagePath).into(viewBinder.ivImage);
            }
            upload();

        }
    }

    private void upload() {
        showLoading(false);
        HttpUtil.update(Api.UPLOAD, new File(imagePath), new HttpUtil.OnNetStringListener() {
            @Override
            public void success(String data, String message) {
                dismissLoading();
                toast(message);
                imagePath = data;
            }

            @Override
            public void error(String error) {
                dismissLoading();
                toast(error);
            }
        });
    }
}